package cn.pconline.autoclub.web;

import cn.pconline.autoclub.config.SystemConfig;
import cn.pconline.autoclub.util.EncodeUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 读取系统配置拦截器
 */
public class SystemConfigInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private SystemConfig systemConfig;

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        if (null == request.getAttribute("cfg")) {
            String ROOT = systemConfig.getRoot();
            request.setAttribute("cfg", systemConfig);
            request.setAttribute("VER_HASH", System.currentTimeMillis());
            request.setAttribute("ROOT", ROOT);         //车友会应用域名
            request.setAttribute("UCROOT", systemConfig.getUcRoot());   //个人中心应用域名
            request.setAttribute("UPCROOT", systemConfig.getUploadRoot());  //上传中心应用域名
            request.setAttribute("BIPROOT", systemConfig.getBipRoot()); //BIP应用域名
            request.setAttribute("PASSPORTROOT", systemConfig.getPassportRoot());   //passport应用域名
            request.setAttribute("BBSROOT", systemConfig.getBbsRoot()); //论坛应用域名
            request.setAttribute("EMOTROOT", systemConfig.getEmotRoot());   //表情域名
            request.setAttribute("TASKROOT", systemConfig.getTaskRoot()); //任务系统域名
            request.setAttribute("APPNAME", systemConfig.getAppName());     //车友会appname
            request.setAttribute("USER_SESSION_NAME", systemConfig.getUserSessionName());     
            request.setAttribute("THIS", ROOT + request.getServletPath());
            request.setAttribute("QUERY_STRING", EncodeUtils.encodeForHTML(request.getQueryString(),2));
            String rootDomain = ROOT.replaceFirst(".+?\\.(pc.+?)\\.com\\.cn$", ".$1.com.cn");
            request.setAttribute("rootDomain", rootDomain);
            request.setAttribute("appName", systemConfig.getAppName()); //和geli兼容
            request.setAttribute("CONTEXT", request.getContextPath());  //和geli兼容
            request.setAttribute("MSGFEEDURL", systemConfig.getMsgFeedUrl());  //应该用不到
            request.setAttribute("SYSNOTICETYPEID", systemConfig.getSysNoticeTpyeId());  //应该用不到
            request.setAttribute("CHECKCODEURL", systemConfig.getCheckCodeUrl());  //应该用不到
            request.setAttribute("BINDPHONEURL", systemConfig.getBindPhoneUrl());  //应该用不到
            request.setAttribute("BBSWAPROOT", systemConfig.getBbsWapRoot());  //应该用不到
            request.setAttribute("UCWAPROOT", systemConfig.getUcWapRoot());  //应该用不到
            request.setAttribute("WAPROOT", systemConfig.getWapRoot()); // wap域名
            request.setAttribute("CLUBDYNATYPE", systemConfig.getClubDynaType());//车友会动态消息类型
            request.setAttribute("JS", ROOT + "/js");
            request.setAttribute("OCLUBROOT", systemConfig.getBrandClubRoot());
            request.setAttribute("PRICEROOT", systemConfig.getPriceRoot());
            request.setAttribute("OCLUBWAPROOT", systemConfig.getBrandClubWapRoot());
        }
        return super.preHandle(request, response, handler);
    }
}
